#include "filemanagedelegate.h"
#include "filemanage.h"
#include "../FtpManage/ftpmanage.h"
#include <QDebug>
#include "../CommunicateDelegate/filedata.h"
#include <QTextCodec>
#include "hmicommondefine.h"
#include "instructionEnum.h"
#include <QMessageBox>

extern FtpManage *ftpManage;
extern FileData fileData;

FileManageDelegate::FileManageDelegate(FileManage *fileManageIn)
{
    fileManage=fileManageIn;
}

FileManageDelegate::~FileManageDelegate()
{

}

void FileManageDelegate::ftpError_slot(QString info)
{
    QMessageBox::warning(NULL, info, "ftp断开，请重新登录控制器！", QMessageBox::Yes );
}

void FileManageDelegate::getFileList_Slot(const QHash<QString, bool> isDirectory, const QString &listFtpDirectory, const QString &listGetFileClientPath)
{
    // 文件模块使用标识
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_FILE_MANAGE)
    {
        return;
    }

    // 获取文件列表
    Q_UNUSED(listFtpDirectory);
    Q_UNUSED(listGetFileClientPath);
    QStringList fileList;

    for(int i = 0; i < isDirectory.count(); i++)
    {
        if(!isDirectory.values().at(i))
        {
            // 编码转换
            fileList << QString::fromLocal8Bit(isDirectory.keys().at(i).toLatin1().data());
        }
    }

//  对远程端文件名进行排序,本地端自动排序
    QStringList list;
    list  = fileList;
    qSort(list.begin(), list.end(), caseInsensitiveLessThan);
    fileList = list;

    // 获取目录信息完成后,关闭FTP操作
    fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
    // 将文件列表更新显示到控件上
    fileManage->updateView_Server(fileList);
}

void FileManageDelegate::downloadFile_Slot()
{
    fileInfoList.clear();
    currentFileIndex = 0;

    // 获取远程文件显示控件上被选择的文件
    fileInfoList = fileManage->getServerFileSelectList();
    if(fileInfoList.isEmpty())
    {
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        return;
    }

    fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
    ftpManage->ftpGetFile(fileInfoList.at(currentFileIndex), localFileDir, remoteFileDir);

}

void FileManageDelegate::uploadFile_Slot()
{
    fileInfoList.clear();
    currentFileIndex = 0;

    // 获取远程文件显示控件上被选择的文件
    fileInfoList = fileManage->getLocalFileSelectList();
    if(fileInfoList.isEmpty())
    {
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        return;
    }
    fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
    ftpManage->ftpPutFile(fileInfoList.at(currentFileIndex), localFileDir, remoteFileDir);


}

void FileManageDelegate::getFileListInfo_Slot(QString dirPath)
{

    fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
    ftpManage->getServerFileListInfo(dirPath);

    remoteFileDir=dirPath;

//    if(isTheSameDir)
//    {
//        QDir filePath;
//        // 本地目录文件信息
//        filePath.setPath(dirPath);
//        foreach (QFileInfo fileInfo, filePath.entryInfoList())
//        {
//            if(!fileInfo.isDir())
//            {
//                fileList << fileInfo.fileName();
//            }
//        }

//        // 更新本地文件列表显示到控件上
//        fileManage->updateView_Local(fileList);


}

void FileManageDelegate::setLocalFileDir_slot(QString dirPath)
{
    localFileDir=dirPath;
}

void FileManageDelegate::isGetFileFinished_Slot(bool status, const QString &message)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_FILE_MANAGE)
    {
        return;
    }
    if(!status)
    {
        qDebug() << "FileManage Get File Failed : " << message;
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        return;
    }
    else
    {
        qDebug() << "FileManage Get File Success : " << message;
        // 当只选择列一个文件时，已下载完成
        if(fileInfoList.count() == 1)
        {
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
            fileManage->updateLocalDir_in();
            return;
        }
        // 下载多个文件时
        else
        {
            currentFileIndex++;
            if(currentFileIndex < fileInfoList.count())
            {
                fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
                ftpManage->ftpGetFile(fileInfoList.at(currentFileIndex), localFileDir, remoteFileDir);

            }
            else
            {
                // 文件全部下载完成时，更新本地文件列表显示到控件上
                fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
                currentFileIndex = 0;
                fileManage->updateLocalDir_in();
                return;
            }
        }
    }
}

void FileManageDelegate::isPutFileFinished_Slot(bool status, const QString &message)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_FILE_MANAGE)
    {
        return;
    }

    if(!status)
    {
        qDebug() << "FileManage Put File Failed : " << message;
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        return;
    }
    else
    {
        qDebug() << "FileManage Put File Success : " << message;
        // 当只选择一个文件时，已上传完成
        if(fileInfoList.count() == 1)
        {
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
            updateServerFileList(remoteFileDir);
            return;
        }
        // 上传多个文件时
        else
        {
            currentFileIndex++;
            if(currentFileIndex < fileInfoList.count())
            {
                fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
                ftpManage->ftpPutFile(fileInfoList.at(currentFileIndex), localFileDir, remoteFileDir);

            }
            else
            {
                //  上传完成后更新显示远程文件列表到控件上
                fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
                currentFileIndex = 0;
                updateServerFileList(remoteFileDir);
                return;
            }
        }
    }
}

void FileManageDelegate::removeLocalFile_Slot()
{
    currentFileIndex = 0;
    removeFilePath = localFileDir;


    // 删除本地文件
    QList <QString> localFileList;
    localFileList = fileManage->getLocalFileSelectList();
    if(!localFileList.isEmpty())
    {
        for(int i=0; i < localFileList.count(); i++)
        {
//            QString tmpCommand=QString("%1%2%3%4%5%6").arg("find ").arg(removeFilePath)
//                    .arg(" -name \"").arg(localFileList.at(i)).arg("\"").arg(" | xargs -d \"\n\" rm");
//            system((tmpCommand.toLatin1().data()));

            QString tmpPath=removeFilePath;
            tmpPath+=localFileList.at(i);
            QFile::remove(tmpPath);
            qDebug()<<"remove "<<tmpPath;
        }
    }

    // 文件全部下载完成时，更新本地文件列表显示到控件上
    fileManage->updateLocalDir_in();


}

void FileManageDelegate::removeRemoteFile_Slot()
{
    currentFileIndex = 0;
    removeFilePath = remoteFileDir;


    // 删除远程文件
    fileInfoList.clear();
    fileInfoList = fileManage->getServerFileSelectList();
    if(!fileInfoList.isEmpty())
    {
        fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
        ftpManage->ftpRemoveFile(fileInfoList.at(0), removeFilePath);
    }
}

void FileManageDelegate::removeFtpFile_Slot(bool status, const QString &message)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_FILE_MANAGE)
    {
        return;
    }
    if(!status)
    {
        qDebug() << "FileManage Remove File Failed :" << message;
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
        return;
    }
    else
    {
        qDebug() << "FileManage Remove File Success :" << message;
        currentFileIndex++;
        if(currentFileIndex < fileInfoList.count())
        {
            // 删除远程下一个文件
            fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
            ftpManage->ftpRemoveFile(fileInfoList.at(currentFileIndex), removeFilePath);
        }
        else
        {
            // 更新远程文件列表显示到控件
            fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
            updateServerFileList(remoteFileDir);
            return;
        }
    }

}


void FileManageDelegate::updateServerFileList(QString dir)
{
    fileData.ftpOperateNum = EM_FTP_OPERATE_FILE_MANAGE;
    ftpManage->getServerFileListInfo(dir);

}

bool FileManageDelegate::caseInsensitiveLessThan(const QString &s1, const QString &s2)
{
    return s1.toLower() < s2.toLower();
}
